package com.sanley.piss.web.service;


import com.sanley.piss.mapper.DictionaryDataMapper;
import com.sanley.piss.mapper.DictionaryTypeMapper;
import com.sanley.piss.model.ServiceImpl;
import com.sanley.piss.model.StringUtil;
import com.sanley.piss.model.exceptionUtil.exception.ServiceException;
import com.sanley.piss.model.po.DictionaryType;
import com.sanley.piss.web.service.mapper.IDictionaryTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: 鸭吼~
 * @Date: 2021-3-16 11:14
 */

@Service
@Primary
public class DictionaryTypeServiceImpl extends ServiceImpl<DictionaryTypeMapper, DictionaryType> implements IDictionaryTypeService {

    @Autowired
    private DictionaryDataMapper dataMapper;

    @Override
    public void insert(DictionaryType dictionaryType) {
        dictionaryType.setStates(0);
        dictionaryType.setGmtCreate(LocalDateTime.now());
        baseMapper.insert(dictionaryType);
    }

    /**
     * 修改信息
     */
    @Override
    public void updateById(DictionaryType dictionaryType) {
        baseMapper.updateById(dictionaryType);
    }

    @Override
    public void removeByIds(Integer[] ids) {
        Arrays.asList(ids).forEach(id->{
            if(StringUtil.isNotNull(dataMapper.selectByType(id))){
                throw new ServiceException("不允许删除");
            }
        });
        baseMapper.removeByIds(ids);
    }

    @Override
    public List<DictionaryType> selectAll() {
        return baseMapper.selectAll();
    }

    @Override
    public DictionaryType selectById(Integer id) {
        DictionaryType selectOne = baseMapper.selectById(id);
        if (StringUtil.isNull(selectOne)) {
            throw new ServiceException("信息不存在");
        }
        return selectOne;
    }
}